package com.reactnativewechatts;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

@ReactModule(name = WechatTsModule.NAME)
public class WechatTsModule extends ReactContextBaseJavaModule {
  public static final String NAME = "WechatTs";

  public static Promise promise;
  public static IWXAPI api = null;
  public static String appId;
  private final static String NOT_REGISTERED = "registerApp required.";

  public WechatTsModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  @NonNull
  public String getName() {
    return NAME;
  }

  @ReactMethod
  public void registerApp(String appid, Promise promise) {
    appId = appid;
    api = WXAPIFactory.createWXAPI(getReactApplicationContext().getBaseContext(),appId, true);
    promise.resolve(api.registerApp(appid));
  }

  @ReactMethod
  public void isWXAppInstalled(Promise promise) {
    if (api == null) {
      promise.reject("Execute error",NOT_REGISTERED);
      return;
    }
    promise.resolve(api.isWXAppInstalled());
  }


  @ReactMethod
  public void getApiVersion(Promise promise) {
    if (api == null) {
      promise.reject("Execute error",NOT_REGISTERED);
      return;
    }
    promise.resolve(api.getWXAppSupportAPI());
  }

  @ReactMethod
  public void openWXApp(Promise promise) {
    if (api == null) {
      promise.reject("Execute error",NOT_REGISTERED);
      return;
    }
    promise.resolve(api.openWXApp());
  }

  @ReactMethod
  public void sendAuthRequest(String scope, String state, Promise promise) {
    if (api == null) {
      promise.reject("Execute error", NOT_REGISTERED);
      return;
    }
    final SendAuth.Req req = new SendAuth.Req();
    req.scope = "snsapi_userinfo,snsapi_friend,snsapi_message,snsapi_contact";
    req.state = "none";
    WechatTsModule.promise = promise;
    api.sendReq(req);
  }

  @ReactMethod
  public void pay(ReadableMap data, Promise promise) {
    WechatTsModule.promise = promise;
    PayReq payReq = new PayReq();
    if (data.hasKey("partnerId")) {
      payReq.partnerId = data.getString("partnerId");
    }
    if (data.hasKey("prepayId")) {
      payReq.prepayId = data.getString("prepayId");
    }
    if (data.hasKey("nonceStr")) {
      payReq.nonceStr = data.getString("nonceStr");
    }
    if (data.hasKey("timeStamp")) {
      payReq.timeStamp = data.getString("timeStamp");
    }
    if (data.hasKey("sign")) {
      payReq.sign = data.getString("sign");
    }
    if (data.hasKey("packageValue")) {
      payReq.packageValue = data.getString("packageValue");
    }
    if (data.hasKey("extData")) {
      payReq.extData = data.getString("extData");
    }
    payReq.appId = appId;
    api.sendReq(payReq);
  }

}
